home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / TransSkel 3.24 / Source / SkelApple.c < prev    next >
Text File  |  1996-01-17  |  4KB  |  146 lines

  1. /*
  2.  * SkelApple.c - TransSkel 3.0 Apple menu handler
  3.  */
  4.  
  5. # include    "TransSkel.h"
  6.  
  7.  
  8. /*
  9.  * Default Apple menu handler support variables
  10.  *
  11.  * appleID is set to skelAppleMenuID if SkelApple() is called and
  12.  * becomes the id of the Apple menu.
  13.  *
  14.  * appleSelect is the procedure to execute if there is a non-DA
  15.  * selection from the Apple menu.
  16.  *
  17.  * appleApplItems is true if the application installs one or more
  18.  * items of its own at the top of the Apple menu.
  19.  */
  20.  
  21. static MenuHandle    appleMenu;
  22. static short        appleID = 0;
  23. static SkelMenuSelectProcPtr appleSelect = (SkelMenuSelectProcPtr) nil;
  24. static Boolean        appleApplItems = false;
  25.  
  26.  
  27. /*
  28.  * Apple menu handler routines
  29.  *
  30.  * If the application installed its own items into the menu and the
  31.  * selection was one of them, pass the item number to the selection
  32.  * procedure.  Otherwise the item is a desk accessory. The port is
  33.  * saved and restored because it is not always preserved correctly
  34.  * across the call to OpenDeskAcc() (IM I-440).
  35.  *
  36.  * DoAppleClobber() disposes of the Apple menu and resets the other
  37.  * Apple menu handler variables.
  38.  */
  39.  
  40. static pascal void
  41. DoAppleItem (short item)
  42. {
  43. GrafPtr    curPort;
  44. Str255    str;
  45. Handle    h;
  46. short    i;
  47.  
  48.     /*
  49.      * If there are application items, determine if item is one of them.
  50.      * Can tell by tracking backward through the items; if the gray line
  51.      * separating application items and DA's if not found before top of
  52.      * menu is reached, then its an application item, else a DA.  This
  53.      * strategy requires that there be no "-" items in the items string
  54.      * passed to SkelApple().
  55.      */
  56.     if (appleApplItems)
  57.     {
  58.         for (i = item; i > 0; i--)    /* look from current to item 1 */
  59.         {
  60.             GetMenuItemText (appleMenu, i, str);
  61.             if (str[0] == 1 && str[1] == '-')
  62.                 break;
  63.         }
  64.         if (i == 0)                    /* reached top without seeing line */
  65.         {
  66.             if (appleSelect != nil)    /* call select proc if there is one */
  67.                 (*appleSelect) (item);
  68.             return;
  69.         }
  70.     }
  71.  
  72.     /* either no application items or selection isn't one of them */
  73.     GetPort (&curPort);
  74.     GetMenuItemText (appleMenu, item, str);            /* get DA name */
  75.     SetResLoad (false);
  76.     h = GetNamedResource ('DRVR', str);
  77.     SetResLoad (true);
  78.     if (h != (Handle) nil)
  79.     {
  80. #if skelUnivHeaders
  81.         ReserveMem (GetResourceSizeOnDisk (h) + 0x1000);
  82. #else
  83.         /* can't use new name for this one in non-universal headers */
  84.         ReserveMem (SizeResource (h) + 0x1000);
  85. #endif
  86.         (void) OpenDeskAcc (str);            /* open it */
  87.     }
  88.     SetPort (curPort);
  89. }
  90.  
  91.  
  92. static pascal void
  93. DoAppleClobber (MenuHandle menu)
  94. {
  95.     DisposeMenu (menu);        /* menu will be == appleMenu here */
  96.     appleID = 0;
  97.     appleApplItems = false;
  98.     appleSelect = nil;
  99. }
  100.  
  101.  
  102. /*
  103.  * Install a handler for the Apple menu.
  104.  *
  105.  * SkelApple() is called if TransSkel is supposed to handle the apple
  106.  * menu itself.
  107.  *
  108.  * items contains the title of any items the application
  109.  * wants to install at the top of the menu.  If items is empty or nil, then
  110.  * only desk accessories are put into the menu.  If not empty, then the items
  111.  * are installed at the top, followed by a gray line, then the desk
  112.  * accessories.
  113.  *
  114.  * doSelect is the procedure to be called when a non-DA selection is
  115.  * made and is passed the item number.  If doSelect is nil, the selection
  116.  * is ignored.
  117.  *
  118.  * SkelApple() does not cause the menubar to be drawn, so if the Apple
  119.  * menu is the only menu, DrawMenuBar() must be called afterward.
  120.  *
  121.  * No value is returned, unlike SkelMenu().  It is assumed that
  122.  * SkelApple() will be called so early in the application that the call
  123.  * to SkelMenu() is virtually certain to succeed.  If it doesn't,
  124.  * presumably there's little hope for the application anyway.
  125.  */
  126.  
  127. pascal void
  128. SkelApple (StringPtr items, SkelMenuSelectProcPtr doSelect)
  129. {
  130.     appleID = skelAppleMenuID;
  131.     /* 024 = apple character */
  132.     appleMenu = NewMenu (appleID, (StringPtr) "\p\024");
  133.     if (items != (StringPtr) nil && items[0] > 0)
  134.     {
  135.         /* application has own items in menu */
  136.         appleApplItems = true;
  137.         /* add items */
  138.         AppendMenu (appleMenu, items);
  139.         /* add gray line */
  140.         AppendMenu (appleMenu, (StringPtr) "\p(-");
  141.         appleSelect = doSelect;
  142.     }
  143.     AppendResMenu (appleMenu, 'DRVR');        /* add desk accessories */
  144.     (void) SkelMenu (appleMenu, DoAppleItem, DoAppleClobber, false, false);
  145. }
  146.